package com.my.spring.framework.context.support;

import com.my.spring.framework.beans.config.MyBeanPostProcessor;
import com.my.spring.framework.context.MyApplicationContext;
import com.my.spring.framework.context.MyApplicationContextAware;

public class MyApplicationContextAwareProcessor implements MyBeanPostProcessor {
    private MyApplicationContext applicationContext;

    public MyApplicationContextAwareProcessor(MyApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws Exception {
        invokeAwareInterfaces(bean);
        return bean;
    }

    private void invokeAwareInterfaces(Object bean) {
        if (bean instanceof MyApplicationContextAware) {
            ((MyApplicationContextAware) bean).setApplicationContext(this.applicationContext);
        }
    }
}
